package com.maycurobj.controller.maycur;

import com.alibaba.fastjson.JSONObject;
import com.maycur.developer.sdk.maycur.ultimate.openapi.request.PostEmployeeBatchRequest;
import com.maycurobj.pojo.EhrEmployeeInfo;
import com.maycurobj.pojo.MayCurPartner;
import com.maycurobj.service.http.HttpAPIService;
import com.maycurobj.service.impl.EhrService;
import com.maycurobj.service.impl.MaycurService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 每刻 往来客商 中台推送
 */
@Controller
@RequestMapping("/maycur/sync")
@Slf4j
public class ManualSyncUserInfoController {

    @Autowired
    private MaycurService maycurService;

    @Autowired
    private EhrService ehrService;

    private Map<String, String> defaultDetpMap = new HashMap<>();

    public ManualSyncUserInfoController() {
        //初始化map数据
        //格式:钉钉id+_+钉钉呼叫部门名称, 每刻部门&&&每刻上级工号&&&本人每刻工号&&&本人姓名&&&本人手机号
//        defaultDetpMap.put("020722623423157211_内蒙大区","DI14210564383315082&&&1015&&&1102&&&王莉莉&&&18698406267");
//        defaultDetpMap.put("020722623423157211_临床学术部","DI14210530818396280&&&1015&&&1102&&&王莉莉&&&18698406267");
//        defaultDetpMap.put("1102_内蒙大区","DI14210564383315082&&&1015&&&1102&&&王莉莉&&&18698406267");
//        defaultDetpMap.put("1102_临床学术","DI14210530818396280&&&1015&&&1102&&&王莉莉&&&18698406267");
//        defaultDetpMap.put("020722623423157211_华北省区","3474219&&&ZJBS000008&&&ZJBS000236&&&王洋生&&&17357156365");
//        defaultDetpMap.put("020722623423157211_助理总经理分管部门","DI22731948369647753&&&ZJBS000008&&&ZJBS000236&&&王洋生&&&17357156365");
//        defaultDetpMap.put("73_华北省区","3474219&&&ZJBS000008&&&ZJBS000236&&&王洋生&&&17357156365");
//        defaultDetpMap.put("73_助理总经理分管部门","DI22731948369647753&&&ZJBS000008&&&ZJBS000236&&&王洋生&&&17357156365");
        defaultDetpMap.put("020722623423157211_内蒙大区","DI14210564383315082&&&1015&&&1102&&&王莉莉&&&18698406267");
        defaultDetpMap.put("020722623423157211_临床学术部","DI14210530818396280&&&1015&&&1102&&&王莉莉&&&18698406267");
        defaultDetpMap.put("1102_内蒙大区","DI14210564383315082&&&1015&&&1102&&&王莉莉&&&18698406267");
        defaultDetpMap.put("1102_临床学术部","DI14210530818396280&&&1015&&&1102&&&王莉莉&&&18698406267");
        defaultDetpMap.put("020722623423157211_华北省区","3474219&&&ZJBS000068&&&ZJBS000236&&&王洋生&&&17357156365");
        defaultDetpMap.put("020722623423157211_山西大区","DI147607757206335740&&&ZJBS000008&&&ZJBS000236&&&王洋生&&&17357156365");
        defaultDetpMap.put("73_华北省区","3474219&&&ZJBS000068&&&ZJBS000236&&&王洋生&&&17357156365");
        defaultDetpMap.put("73_山西大区","DI147607757206335740&&&ZJBS000008&&&ZJBS000236&&&王洋生&&&17357156365");
        defaultDetpMap.put("020722623423157211_福建省区","96005048&&&ZJBS000180&&&ZJBS000242&&&黄剑川&&&17681887966");
        defaultDetpMap.put("020722623423157211_豫鄂省区","95938049&&&ZJBS000031&&&ZJBS000242&&&黄剑川&&&17681887966");
        defaultDetpMap.put("74_福建省区","96005048&&&ZJBS000180&&&ZJBS000242&&&黄剑川&&&17681887966");
        defaultDetpMap.put("74_豫鄂省区","95938049&&&ZJBS000031&&&ZJBS000242&&&黄剑川&&&17681887966");
    }

    /**
     * 接受手动推送对象
     * 可以不用
     * empcode: xxxx(需要推送的工号)
     */
    @PostMapping("/manualPushUser")
    @Deprecated
    @ResponseBody
    public Map<String, String> push(@RequestBody JSONObject userInfo) {
        Map<String, String> retmap = new HashMap<>();
        try {
            if(StringUtils.isBlank(userInfo.getString("empcode"))){
                retmap.put("msg", "参数异常(empcode)");
                return retmap;
            }
            Map<String,Object> map=ehrService.getEHRUserInfo();
            List<EhrEmployeeInfo> items = (List<EhrEmployeeInfo>) map.get("ehrObject");

            String empInfo = userInfo.getString("empcode");
            maycurService.employEhrToMayCurByUserId(empInfo,items);
            retmap.put("msg", "推送完成");
            return retmap;
        } catch (Exception e) {
            e.printStackTrace();
            retmap.put("msg", "程序异常");
            return retmap;
        }
    }

    /**
     * 请求每刻 用户部门信息
     * @param employeeId
     * @return
     */
    @PostMapping("/getEmployeeDeptName")
    @ResponseBody
    public Map<String, Object> getEmployeeDeptName(String employeeId){
        Map<String, Object> retMap = new HashMap<>();

        if (StringUtils.isBlank(employeeId)) {
            retMap.put("code", -1);
            retMap.put("result", "请求非法");
            return retMap;
        }
        String deptName=maycurService.getEmployeeDeptName(employeeId);
        retMap.put("code", 0);
        retMap.put("result", deptName);
        return retMap;
    }

    /**
     * 钉钉修改 每刻用户大区
     * @param uid
     */
    @PostMapping("/aiChangeUserDept")
    @ResponseBody
    public String aiChangeUserDept(String uid) {
        if (StringUtils.isBlank(uid)) {
            log.info("-----aiChangeUserDept--uid空的");
        }
        //根据传入条件,controller层处理用户
        if (!defaultDetpMap.containsKey(uid)) {
            return "更新失败";
        }
        String mapRet = defaultDetpMap.get(uid);
        String[] mapRets = mapRet.split("&&&");
        String businessCode=mapRets[0];
        String supervisorEmployeeId=mapRets[1];
        String employeeCode = mapRets[2];
        String employeeName = mapRets[3];
        String employeePhone = mapRets[4];
        //请求调整
        List<PostEmployeeBatchRequest.User> reqUserList = new ArrayList<>();
        PostEmployeeBatchRequest.User user = new PostEmployeeBatchRequest.User();
        List<PostEmployeeBatchRequest.RTRoute> rtRoutes = new ArrayList<>();
        PostEmployeeBatchRequest.RTRoute rtRoute = new PostEmployeeBatchRequest.RTRoute();
        user.setEmployeeId(employeeCode);
        user.setEmployeeName(employeeName);
        user.setPhoneNo(employeePhone);
        rtRoute.setBusinessCode(businessCode);
        rtRoute.setSupervisorEmployeeId(supervisorEmployeeId);
        rtRoute.setDefaultDepartment(true);
        rtRoutes.add(rtRoute);
        user.setDepartments(rtRoutes);
        reqUserList.add(user);
        try {
            maycurService.pushMaycurEmployeeFunction(reqUserList);
            return "完成";
        }catch (Exception ex) {
            ex.printStackTrace();
            return "异常";
        }
    }
}
